#ifndef SLIST_H_
#define SLIST_H_

#include <stddef.h>
#include <list.h>

#ifdef __cplusplus
extern "C" {
#endif
	
typedef struct slist_s {
	struct slist_s *next;
}slist_t;

#define SLIST_HEAD_INIT(name)	{&name}

#define slist_entry(ptr, type, member)   container_of(ptr, type, member)

static inline void slist_init_head(slist_t *head)
{
	head->next = head;
}

static inline void slist_insert(slist_t *new_, slist_t *prev, slist_t *next)
{
	prev->next = new_;
	new_->next = next;
}

static inline void slist_add(slist_t *new_, slist_t *head)
{	
	slist_insert(new_, head, head->next);
}

static inline void slist_del(slist_t *prev, slist_t *node)
{
	prev->next = node->next;
}

static inline void slist_del_init(slist_t *prev, slist_t *node)
{
	slist_del(prev, node);
	slist_init_head(node);
}

static inline int slist_is_last(const slist_t *list,
        const slist_t *head){
    return list->next == head;
}

static inline int slist_empty(const slist_t *head){
    return head->next == head;
}

#define slist_for_each_entry_safe(pos, n, head, member)      			\
    for (pos = slist_entry((head)->next, typeof(*pos), member),      	\
         n = slist_entry(pos->member.next, typeof(*pos), member); 		\
         &pos->member != (head);                                    	\
         pos = n, n = slist_entry(n->member.next, typeof(*n), member))
	
#ifdef __cplusplus
}
#endif

#endif
